package com.supermap.authentication.common;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;



//读取配置文件（属性文件）的工具类
public class ReadXML {
	private static ReadXML readXml;
	private static SAXReader reader;
	private  Map<String, String> map = new HashMap<String, String>();
	private ReadXML() {
		map =this.readXml();
	}

	@SuppressWarnings("unchecked")
	private Map<String, String> readXml() {
		try {
			reader = new SAXReader();
			String configFile = ReadXML.class.getClassLoader().getResource("config/web-service-param.xml").getPath();
			configFile=URLDecoder.decode(configFile,"utf-8");
			InputStream in = new FileInputStream(configFile);
			InputStreamReader isr = null;
			List<Element> coutryList = null;
			isr = new InputStreamReader(in, "UTF-8");
			Document document = reader.read(isr);
			Element rootElement = document.getRootElement();
			coutryList = rootElement.elements();
			for (Element country : coutryList) {
				List<Element> stateList = country.elements();
				for (Element state : stateList) {
					String key = state.getName();
					String value = state.getText();
					map.put(key, value);
					
				}
			}
		}  catch (Exception e) {
			e.printStackTrace();
		} 
		return map;
	}

	public static ReadXML getInstance() {
		if (readXml == null) {
			readXml = new ReadXML();
		}
		return readXml;
	}

	public Map<String, String> getList() {
		return map;

	}
	
	public static void main(String[] args) {
		ReadXML.getInstance().readXml();
	}
}
